package com.zr.legalfactory.controller;

import com.zr.legalfactory.pojo.LegalFactoryAddVo;
import com.zr.legalfactory.pojo.LegalFactorySelectVo;
import com.zr.legalfactory.pojo.LegalFactoryStatusVo;
import com.zr.legalfactory.pojo.LegalFactoryUpdateVo;
import com.zr.legalfactory.serevice.LegalFactoryService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Administrator on 2019/6/15.
 */
@RestController
public class LegalFactoryController {

    @Autowired
    private LegalFactoryService legalFactoryService;

    /**
     * 添加
     * @param legalFactoryAddVo
     * @param bindingResult
     * @return
     */

    @PostMapping("addLegalFactory")
    public ResultVo addLegalFactory(@RequestBody @Valid LegalFactoryAddVo legalFactoryAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return legalFactoryService.addLegalFactory(legalFactoryAddVo);
    }

    /**
     * 修改
     * @param legalFactoryUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("updateFactory")
    public ResultVo updateFactory(@RequestBody @Valid LegalFactoryUpdateVo legalFactoryUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return legalFactoryService.updateFactory(legalFactoryUpdateVo);
    }
//
    /**
     * 查看
     */
    @GetMapping("/queryFactoryByFid")
    public ResultVo queryFactoryByFid(@RequestParam("fid")Integer fid){

        return legalFactoryService.queryFactoryByFid(fid);
    }
//
    /**
     * 分页查询
     */
    @PostMapping("queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody LegalFactorySelectVo legalFactorySelectVo){

        return legalFactoryService.queryPage(legalFactorySelectVo);
    }



    @PostMapping("updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid LegalFactoryStatusVo legalFactoryStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return legalFactoryService.updateStatus(legalFactoryStatusVo);
    }

}
